package top.deepdesigner.controller.market;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsCategory;
import top.deepdesigner.service.DtsCategoryService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CatVo;
import top.deepdesigner.vo.CommonListVO;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 19:27:43 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/category")
public class CategoryController {

  @Autowired
  private DtsCategoryService categoryService;

  @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:category:list"})
  @GetMapping("/list")
  public Object list(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "20") Integer limit,
                     @RequestParam(defaultValue = "add_time") String sort,
                     @RequestParam(defaultValue = "desc") String order,
                     @RequestParam(defaultValue = "") String id,
                     @RequestParam(defaultValue = "") String name) {

    CommonListVO<DtsCategory> categoryListVO = new CommonListVO<>();
    //获取分页数量
    Long count = categoryService.getCategoryCount();
    List<DtsCategory> dtsCategories = categoryService.getCategoryList(page, limit, sort, order, id, name);
    categoryListVO.setTotal(count);
    categoryListVO.setItems(dtsCategories);
    return ResponseUtil.ok(categoryListVO);
  }

  /**
   * 商品一级类目列表
   * 接口地址 : http://localhost:8083/admin/category/l1
   *
   * @return {@link Object}
   */
  @GetMapping("/l1")
  public Object l1() {
    List<CatVo> l1 = categoryService.getCategoryLevel("L1");
    return ResponseUtil.ok(l1);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "添加")
  @RequiresPermissions(value = {"admin:category:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsCategory category) {
    DtsCategory categoryRes = categoryService.createCategory(category);
    if (category != null) {
      return ResponseUtil.ok(categoryRes);
    }
    return ResponseUtil.fail();
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "编辑")
  @RequiresPermissions(value = {"admin:category:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsCategory category) {
    Boolean aBoolean = categoryService.updateCategory(category);
    if (aBoolean) {
      return ResponseUtil.ok();
    }
    return ResponseUtil.fail();
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "商品类目"}, button = "删除")
  @RequiresPermissions(value = {"admin:category:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsCategory category) {
    Boolean aBoolean = categoryService.deleteCategory(category);
    if (aBoolean) {
      return ResponseUtil.ok();
    }
    return ResponseUtil.fail();
  }
}
